package cn.xi.goods.controller;

import cn.xi.goods.service.BrandService;
import cn.xi.pojo.Brand;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
@CrossOrigin  //开启跨域请求
public class BrandController {
    @Autowired
    private BrandService brandService;

    //    查询所有
    @GetMapping
    public Result<Brand> findAll() {
        List<Brand> all = brandService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", all);
    }


    //   根据id查询
    @GetMapping("/{id}")
    public Result<Brand> findId(@PathVariable Integer id) {
        Brand b = brandService.findId(id);
        return new Result<>(true, StatusCode.OK, "查询成功", b);
    }

    //   新增数据
    @PostMapping()
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    //根据id修改数据
    @PutMapping("/{id}")
    public Result save(@PathVariable Integer id, @RequestBody Brand brand) {
        brand.setId(id);
        brandService.save(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //    根据id删除数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    // 条件查询
    @GetMapping("/search")
    public Result<List<Brand>> getString(@RequestParam Map map) {
        return brandService.getString(map);

    }

    //    分页查询
    @GetMapping("/page/{page}/{size}")
    public Result<Brand> pageFind(@PathVariable("page") Integer page, @PathVariable Integer size) {
        PageResult<Brand> brandPageResult = brandService.pageFind(page, size);
        return new Result<>(true, StatusCode.OK, "分页查询成功", brandPageResult);
    }


    //    分页条件查询
    @GetMapping("/search/page/{page}/{size}")
    public Result<List<Brand>> getPageString(@RequestParam Map map, @PathVariable("page") Integer page, @PathVariable Integer size) {
        return brandService.getPageString(map, page, size);

    }


    @GetMapping("/catbrand/{name}")
    public Result<List<Brand>> catbrand(@PathVariable("name") String name){
        List<Map> categoryByBrand = brandService.findCategoryByBrand(name);
        return new Result<>(true,StatusCode.OK,"查询成功",categoryByBrand);
    }
}
